package com.hh.front.config;

import com.hh.common.interceptor.SwaggerInterceptor;
import com.hh.front.filter.ResponseFilter;
import com.hh.front.interceptor.FrontTokenInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.MappedInterceptor;

/**
 * token验证拦截器
 * +----------------------------------------------------------------------
 * | hh [ hh赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2022 https://www.hh.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed hh并不是自由软件，未经许可不能去掉hh相关版权
 * +----------------------------------------------------------------------
 * | Author: hh Team <admin@hh.com>
 * +----------------------------------------------------------------------
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    // 这里使用一个Bean为的是可以在拦截器中自由注入，也可以在拦截器中使用SpringUtil.getBean 获取
    // 但是觉得这样更优雅

    @Bean
    public HandlerInterceptor frontTokenInterceptor(){
        return new FrontTokenInterceptor();
    }

    @Bean
    public ResponseFilter responseFilter(){ return new ResponseFilter(); }


    private String username;

    private String password;
    
    private Boolean check;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加token拦截器
        //addPathPatterns添加需要拦截的命名空间；
        //excludePathPatterns添加排除拦截命名空间

        //前端用户登录token
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(frontTokenInterceptor()).
                addPathPatterns("/api/front/**").
                excludePathPatterns("/api/front/index",
                        "/api/front/test",
                        "/api/front/jingangArea",
                        "/api/front/qrcode/**",
                        "/api/front/login/mobile",
                        "/api/front/login",
                        "/api/front/sendCode",
                        "/api/front/wechat/**",
                        "/api/front/search/keyword",
                        "/api/front/share",
                        "/api/front/article/**",
                        "/api/front/city/**",
                        "/api/front/product/hot",
                        "/api/front/product/good",
                        "/api/front/products/**",
                        "/api/front/reply/**",
                        "/api/front/user/service/**",
                        "/api/front/logistics",
                        "/api/front/groom/list/**",
                        "/api/front/config",
                        "/api/front/category",
                        "/api/front/seckill/*",
                        "/api/front/seckill/list/*",
                        "/api/front/seckill/detail/*",
                        "/api/front/ios/*",
                        "/api/front/ios/register/binding/phone",
                        "/api/front/combination/index",
                        "/api/front/seckill/index",
                        "/api/front/bargain/index",
                        "/api/front/combination/index",
                        "/api/front/index/product/*",
                        "/api/front/index/color/config",
                        "/api/front/image/domain",
                        "/api/front/product/leaderboard",
                        "/api/front/wx/authLogin",
                        "/api/front/payment/callback/*",
                        "/api/front/page/recommendList",
                        "/api/front/im/registerImUser",
                        "/api/front/maintenance/systemMaintenance");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public FilterRegistrationBean filterRegister()
    {
        //注册过滤器
        FilterRegistrationBean registration = new FilterRegistrationBean(responseFilter());
        registration.addUrlPatterns("/*");
        return registration;
    }

    /* 必须在此处配置拦截器,要不然拦不到swagger的静态资源 */
    @Bean
    @ConditionalOnProperty(name = "swagger.basic.enable", havingValue = "true")
    public MappedInterceptor getMappedInterceptor() {
        return new MappedInterceptor(new String[]{"/doc.html", "/webjars/**"}, new SwaggerInterceptor(username, password, check));
    }
}
